$(function () {
    obj.init();
});

var obj = new function () {
    return {
        dataGrid: null,
        dom: {
            search: null
        },
        handleData: {//添加教辅操作数据
            school: null,
            application: null,
            class_data: null,
            subject: null,
            teacher: null
        },
        handleData_Back: {//撤销教辅操作数据
            school: null,
            application: null,
            class_data: null,
            subject: null,
            teacher: null
        },
        init: function () {
            this.dataGrid = new xhDataGrid($('#list'), 'Res/getNewBookList',{type:1});
            this.dataGrid.parseToolbar().init().activeButton([],['bt_list_back']);
            this.dom.search = $('#bt_list_search');
            // this.funObj().search();
            this.listener();
        },
        listener: function () {
            var $this = this;
            //查询
            $this.dom.search.click(function () {
                $this.funObj().search();
            });
            xhTool.bindDocKeyDown($this.dom.search);
            //关联题库
            $('#bt_list_contact').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                window.top.xhTop.showModal({
                    title: '选择教师',
                    content: xhTool.getFrameString('Res/teacherContactBook', {
                        nav: false,
                        bookId: $this.dataGrid.getSelectValue('bookId'),
                        subject:escape($this.dataGrid.getSelectValue('subject'))
                    }, 500),
                    size: 'lg',
                    backClose:false
                });
            });
            //进程
            $('.xh-datagrid').on('click','.progress',function () {
                var bookId=$(this).attr('bookId');
                var host=$(this).attr('host');
                window.top.xhTop.showModal({
                    width:'800',
                    title:'查看进度',
                    content: '<iframe src="'+host+'/Cloudwk/getBookCatelog?bookId='+bookId+'"  style="width: 100%;;border-style: none;padding: 0px;height:520px"  scrolling="yes"   ></iframe>'
                });
            });
            $('#list_search_grade').combobox({
                onSelect: function(param){
                    var url = 'getSearchData?type=1&grade='+param.value;
                    $('#list_search_subject').combobox('reload', url);
                    $('#list_search_subject').combobox('setValue', '--请选择--');
                    $('#list_search_book').combobox('setValue', '--请选择--');
                    $('#list_search_book').combobox('loadData', {"total":0,rows:[] });
                    $('#list_search_book').combobox('setValue', '--请选择--');
                    $this.funObj().clearHandleData();//清除操作记录
                }
            });
            $('#list_search_subject').combobox({
                onSelect: function(param){
                    var grade = xhExtra.getValue($('#list_search_grade'));
                    var url = 'getSearchData?type=2&subject='+param.value+'&grade='+grade;
                    $('#list_search_book').combobox('reload', url);
                    $('#list_search_book').combobox('setValue', '--请选择--');
                    $this.funObj().clearHandleData();//清除操作记录
                }
            });
            $('#list_search_book').combobox({
                onSelect: function(param){
                    $this.funObj().clearHandleData();//清除操作记录
                }
            });
            //增加
            $('#bt_list_add').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().addApply();
            });
            //撤销
            $('#bt_list_back').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().backApply();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                //增加
                addApply: function () {
                    var bookId = $this.dataGrid.getSelectValues('bookId');
                    var bookName = $this.dataGrid.getSelectValues('bookName');
                    var app = xhExtra.getValue($('#list_search_app'));
                    if (bookId.length > 1){
                        window.top.xhTop.showError('请单选后操作');
                        return false;
                    }
                    window.top.xhTop.showModal({
                        title: '添加教辅申请',
                        content: xhTool.getFrameString('res/addReferenceApply', {bookId: bookId, bookName: bookName, app: app}, 500),
                        button: '保存',
                        size: 'lg',
                        width:'800',
                        backClose: false
                    });
                },
                //撤销
                backApply: function () {
                    var bookId = $this.dataGrid.getSelectValue('bookId');
                    var bookName = $this.dataGrid.getSelectValue('bookName');
                    if (bookId < 1 || bookId === null || bookId === ''){
                        window.top.xhTop.showError('请选择后操作');
                        return false;
                    }
                    window.top.xhTop.showModal({
                        title: '填写教辅撤销申请信息',
                        content: xhTool.getFrameString('res/backReferenceApply', {bookId: bookId, bookName: bookName}, 250),
                        button: '保存',
                        size: 'lg',
                        width:'800',
                        backClose: false
                    });
                },
                search: function () {
                    if (xhExtra.getValue($('#list_search_grade')) < 1 || xhExtra.getValue($('#list_search_grade')) == '--请选择--'){
                        window.top.xhTop.showMessage({
                            title: "请选择年级",
                            text:  '',
                            type: 'error'
                        });
                        return false;
                    }

                    if (xhExtra.getValue($('#list_search_subject')) < 1 || xhExtra.getValue($('#list_search_subject')) == '--请选择--'){
                        window.top.xhTop.showMessage({
                            title: "请选择科目",
                            text:  '',
                            type: 'error'
                        });
                        return false;
                    }
                    if (xhExtra.getValue($('#list_search_book')) < 1 || xhExtra.getValue($('#list_search_book')) == '--请选择--'){
                        window.top.xhTop.showMessage({
                            title: "请选择教材",
                            text:  '',
                            type: 'error'
                        });
                        return false;
                    }
                    // if (xhExtra.getValue($('#list_search_app')) < 1 || xhExtra.getValue($('#list_search_app')) == '--请选择--'){
                    //     window.top.xhTop.showMessage({
                    //         title: "请选择应用",
                    //         text:  '',
                    //         type: 'error'
                    //     });
                    //     return false;
                    // }
                    var search = xhExtra.getValue($('#list_search_vague'));
                    var search_type = xhExtra.getValue($('#list_search_type'));
                    if (search === ""){
                        window.top.xhTop.showMessage({
                            title: "请输入'搜索'内容",
                            text:  '',
                            type: 'error'
                        });
                        return false;
                    }

                    if (!search_type) {
                        find = /[\u4E00-\u9FA5]/g;
                        if (find.test(search) && search.match(find).length < 4){//如果是中文不能少于四个字
                            window.top.xhTop.showMessage({
                                title: "'搜索'书名不能少于四个字",
                                text:  '',
                                type: 'error'
                            });
                            return false;
                        }
                    }

                    $this.dataGrid.reload({
                        search: {
                            vague: search
                        },
                        book: xhExtra.getValue($('#list_search_book')),
                        grade: xhExtra.getValue($('#list_search_grade')),
                        subject: xhExtra.getValue($('#list_search_subject')),
                        search_type: xhExtra.getValue($('#list_search_type')),
                        app: xhExtra.getValue($('#list_search_app')),
                        type: 2
                    });
                },
                changeStage: function (stage) {
                    $('.stage').hide().find('select').attr('disabled', true);
                    $('.stage-' + stage).fadeIn().find('select').attr('disabled', false);
                },
                clearHandleData: function () {
                    $this.handleData.school = $this.handleData.application = $this.handleData.class_data = $this.handleData.subject = $this.handleData.teacher = null;
                    $this.handleData_Back.school = $this.handleData_Back.application = $this.handleData_Back.class_data = $this.handleData_Back.subject = $this.handleData_Back.teacher = null;
                }
            }
        }
    }
};

